/*
https://leetcode-cn.com/problems/maximum-product-of-word-lengths/solution/zui-da-dan-ci-chang-du-cheng-ji-by-leetc-lym9/
 */
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Solution318 {
    public int maxProduct(String[] words) {
        Map<Integer,Integer> map=new HashMap<>();
        for (int i=0;i<words.length;i++){
            int hash=0;
            char[] chars=words[i].toCharArray();
            for (char aChar : chars) {
                hash=hash|(1<<(aChar-'a'));
            }
            if (chars.length>map.getOrDefault(hash,0)){
                map.put(hash,chars.length);
            }
        }
        Set<Integer> set=map.keySet();
        int ans=0;
        for (int i1 : set) {
            for (int i2 : set) {
                if ((i1 & i2)==0){
                    ans=Math.max(ans,map.get(i1)*map.get(i2));
                }
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution318().maxProduct(new String[]{"abcw","baz","foo","bar","xtfn","abcdef"}));
    }
}
